// Copyright 2012. InMobi. All Rights reserved

package com.inmobi.sharedlock;

/**
 *
 */
public class ThreadSafeAverageCalculator implements AverageCalculator {

  private final Object monitor = new Object();

  private long sum = 0;

  private int count = 0;

  @Override
  public void addValue(long value) {
    synchronized (monitor) {
      count++;
      sum+= value;
    }
  }

  @Override
  public double getAverage() {
    synchronized (monitor) {
      return count != 0 ? ((double)sum) / count : 0.0;
    }
  }
}
